<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.0, user-scalable=0,shrink-to-fit=no, viewport-fit=cover">
    <meta name="keywords" content="蓝桥到家">
    <meta name="description" itemprop="description" content="生鲜副食、饮品乳品、鲜花蛋糕">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>确认订单</title>
    <link rel="stylesheet" type="text/css" href="../styles/reset.css" />
    <link rel="stylesheet" type="text/css" href="../styles/iconfont/iconfont.css" />
    <link rel="stylesheet" type="text/css" href="../styles/order-confirm.css" />
    <link rel="stylesheet" type="text/css" href="../styles/common.css" />
    <script type="text/javascript" src="../js/common.js"></script>
    <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
    <script>
        window.onload = function (){
            const {createApp, ref,computed} = Vue
            createApp({
                setup() {
                    const shopCarList = ref([])
                    const deliveryFee = 6 //配送费固定

                    // 2. 获取数据后初始化count（确保每个商品有数量属性）
                    let getData = () => {
                        fetch(`http://localhost:3000/shopCar`)
                            .then(response => response.json())
                            .then(data => {
                                shopCarList.value = data[0]
                            })
                            .catch(err => console.error('获取数据失败:', err));
                    }
                    getData()

                    const sumPrice =  computed(() =>{
                        let sum = deliveryFee;
                        for (let i = 1; i < shopCarList.value.shopCarGoods.length+1; i++) {
                            sum = Number.parseFloat((shopCarList.value.shopCarGoods[i-1].count * shopCarList.value.shopCarGoods[i-1].price).toFixed(2)) + sum;
                        }
                        return sum;
                    })

                    return {shopCarList,sumPrice,deliveryFee}
                }
            }).mount('#app')
        }
    </script>
</head>

<body>
    <div class="wrap" id="app">
        <!-- 头部 start -->
        <header class="header_bar">
            <a href="shopcart.html">
                <div class="header_bar_back"></div>
            </a>
            <div class="header_bar_title">确认订单</div>
        </header>
        <!-- 头部 end -->
        <div class="container">
            <!-- 订单信息 start -->
            <div class="order-address">
                <h5>配送至：</h5>
                <a href="address.html">
                    <div class="address">
                        <p>{{shopCarList.address.city+shopCarList.address.department+shopCarList.address.houseNumber}}</p>
                        <p>{{shopCarList.address.name}} {{shopCarList.address.phone}}</p>
                        <span class="iconfont icon-youjiantou"></span>
                    </div>
                </a>
            </div>

            <h3>永辉超市</h3>

            <ul class="order-detail">
                <li v-for = "(item,index) in shopCarList.shopCarGoods" :key="index">
                    <div class="goods">
                        <img :src="item.imgUrl" alt="">
                        <p>{{ item.names }} x {{ item.count }}</p>
                    </div>
                    <p>&#165; {{ (item.price * item.count).toFixed(2) }}</p>
                </li>
            </ul>

            <div class="order-cost">
                <p>配送费</p>
                <p>&#165; {{ deliveryFee }}</p>
            </div>

            <!-- 合计部分 -->
            <div class="total">
                <div class="total-left">
                    总计：{{ sumPrice }}元
                </div>
                <div class="total-right" onclick="location.href='payment.html'">
                    去支付
                </div>
            </div>
            <!-- 订单信息 end -->
        </div>
    </div>
</body>

</html>